#include "WebSocketClient.h"

template<typename Msg, typename CallBack>
WebSocketClient<Msg, CallBack>::WebSocketClient(std::shared_ptr<CallBack> callBack)
    :callBack_(callBack)
{
}

template<typename Msg, typename CallBack>
void WebSocketClient<Msg, CallBack>::run(const std::string host, const std::string port, const std::string target){
    host_ = host;
    text_ = text;

    // Look up the domain name
    resolver_.async_resolve(
        host,
        port,
        beast::bind_front_handler(
            &session::onResolve,
            shared_from_this()));
}

template<typename Msg, typename CallBack>
void WebSocketClient<Msg, CallBack>::onResolve(std::error_code ec, tcp::resolver::results_type results){
      if(ec){
         return fail(ec, "resolve");
      }
           

        // Set the timeout for the operation
        beast::get_lowest_layer(ws_).expires_after(std::chrono::seconds(30));

        // Make the connection on the IP address we get from a lookup
        beast::get_lowest_layer(ws_).async_connect(
            results,
            beast::bind_front_handler(
                &session::on_connect,
                shared_from_this()));
}
